using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NiSis.Loggin;
//using MSLogging = Microsoft.Practices.EnterpriseLibrary.Logging;

namespace NiSis
{
    public partial class FakeLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LogUser();
        }
        protected void LogUser()
        {
            string susr = GetUser();
            //string susr = "pippo";
            MembershipUser usr =Membership.GetUser(susr);

            if (usr != null)
            {
                Logger.Conferma(string.Format("Operazione: utente logato {0}, esito: positivo", susr));
                //Conferma(string.Format("utente logato {0}", susr));
                FormsAuthentication.RedirectFromLoginPage(susr, true);
            }
            else
                Logger.Conferma(string.Format("Operazione: login utenza {0},esito: negativo",susr)); 

        }
        protected string GetUser()
        {
            string susr = ConfigurationManager.AppSettings["TestUser"] ?? string.Empty;
            if (susr==string.Empty)
                susr =Request["usr"]==null? System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString():Request["usr"];
            string[] mat = susr.Split('\\');
            if (mat.Length == 0)
                return string.Empty;

            return mat[mat.Length - 1];
        }
        
        //public  void Conferma(string message)
        //{
        //    MSLogging.LogEntry log = new MSLogging.LogEntry();
        //    log.Message = message;
        //    log.Message = "Conferma";
        //    log.Priority = 1;

        //    MSLogging.Logger.Write(log);
        //}

    }
}
